package genosAgape.ui;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class SplashWindow extends JWindow {
    
    private static SplashWindow instance;
	private boolean paintCalled = false;
    
	private SplashWindow(JPanel parent) {
        super();
        
        setContentPane(parent);
        parent.setLayout(null);
        parent.setOpaque(false);
                
        JLabel load = new JLabel("");
		load.setIcon(new ImageIcon(SplashWindow.class.getResource("/genosAgape/ui/img/load.gif")));
		load.setBounds(70, 120, 160, 24);
		parent.add(load);
		
		JLabel imgSplasher = new JLabel("");
		imgSplasher.setIcon(new ImageIcon(SplashWindow.class.getResource("/genosAgape/ui/img/splashImage.png")));
		imgSplasher.setBounds(0, 0, 640, 300);
		parent.add(imgSplasher);
		        
        // Center the window on the screen
        int imgWidth = 640;
        int imgHeight = 300;
        setSize(imgWidth, imgHeight);        
        Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(
        		(screenDim.width - imgWidth) / 2,
        		(screenDim.height - imgHeight) / 2
        );
                
        /*MouseAdapter disposeOnClick = new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                synchronized(SplashWindow.this) {
                    SplashWindow.this.paintCalled = true;
                    SplashWindow.this.notifyAll();
                }
                dispose();
            }
        };
        addMouseListener(disposeOnClick);*/
    }
    
    /**
     * Updates the display area of the window.
     */	
    public void update(Graphics g) {
        paint(g);
    }
    /**
     * Paints the image on the window.
    */
    public void paint(Graphics g) {    	
    	super.paint(g);
        if (! paintCalled) {
            paintCalled = true;
            synchronized (this) { notifyAll(); }
        }
    }
    
    /**
     * Open's a splash window using the specified image.
     * @param image The splash image.
     */
    public static void splash() {
        if (instance == null) {
            JPanel f = new JPanel();
            instance = new SplashWindow(f);
            instance.setVisible(true);        
            
            if (! EventQueue.isDispatchThread()
            && Runtime.getRuntime().availableProcessors() == 1) {
                synchronized (instance) {
                    while (! instance.paintCalled) {
                        try { instance.wait(); } catch (InterruptedException e) {}
                    }
                }
            }
        }
    }    
    
    /**
     * Closes the splash window.
     */
    public static void disposeSplash() {
        if (instance != null) {
            instance.getOwner().dispose();
            instance = null;
        }
    }
    
    /**
     * Invokes the main method of the provided class name.
     * @param args the command line arguments
     */
    public static void invokeMain(String className, String[] args) {
        try {
            Class.forName(className)
            .getMethod("main", new Class[] {String[].class})
            .invoke(null, new Object[] {args});
        } catch (Exception e) {
            InternalError error = new InternalError("Failed to invoke main method");
            error.initCause(e);
            throw error;
        }
    }
}
